#'
#' @TODO 普适function
#' @title 普适function
#' @description 根据你输入的变量，以及自定义的funcion，进行计算结果，返回的是一个function。估计这么写不对，应该是有个函数工厂那样的类型。
#' @export 
#' @return a function
#' @examples
#' set.seed(33)
#' foo <- general_FUN(a = 12, b = 3, d = 4, e = 3, function() {
#'    z <- sample(seq_along(a),b,replace = T)
#'    (a * b + a / e) / a ^ z[1]
#' })
#' foo()
#' [1] 10  8  6
#' [1] 33.33333
#' @author *WYK*
general_FUN <- function(...) {
    f_tmp <- \(...){}
    lst_inner <- list(...)
    for (i in names(lst_inner)) {
        if (i != "") {
            assign(i, lst_inner[[i]])
        }
    }

    body(f_tmp) <- body(lst_inner[[which(map_lgl(lst_inner, ~ is.function(.x)))]])
    return(f_tmp)
}


# my_sum = function(x, y, z){
#     return(x + y + z)
# }
# sqr_sum = function(...){
#     args = as.list(match.call())[-1]
#     args = lapply(args, function(x) x^2)
#     do.call(my_sum, args)
# }
# sqr_sum(x = 2, y = 3, z = 4)
# # [1] 29

# sqr_sum = function(...){
#     args = as.list(match.call())[-1]
#     args = lapply(args, function(x) x^2)
#     do.call(my_sum, args)
# }


# sqr_sum = function(...){
#     args = as.list(match.call())[-1]
#     call.envir = parent.frame(1)
#     args = lapply(args, function(arg){
#         if(is.symbol(arg)){
#             eval(arg, envir = call.envir)
#         } else {
#             arg
#         }
#     })
#     args = lapply(args, function(x) x^2)
#     do.call(my_sum, args)
# }


# foo <- function(a, b) {
#     function(d) {
#         a + b + d
#     }
# }
# foo(1,2)(12)


# foo <- function(a, b) {
#     a + b
# }

# foo1 <- function(a, b) {
#     a - b
# }


# bar <- function(func) {
#     function(...) {
#         print('a')
#         func(...)
#     }
# }

# bar(foo)(1, 2)
# bar(foo1)(1, 2)

# bar <- function() {
#     function(a, b) {
#         print(a)
#         foo(a, b)
#     }
# }
